<template>
  <el-row class="login-page">
    <el-col :span="16" class="bg">
      <div class="title">智界峨眉</div>
    </el-col>
    <el-col :span="6" :offset="1" class="form">
      <el-form
        v-if="isRegister"
        :model="register"
        :rules="rules"
        size="large"
        autocomplete="off"
      >
        <el-form-item>
          <h1 class="form-title">申请账号</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="register.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="register.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="register.code"
            :prefix-icon="Lock"
            type="code"
            placeholder="请输入邀请码"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="button" @click="registerAPI"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link
            @click="
              isRegister = false;
              clearData();
            "
            :underline="false"
            class="switch-link"
            >已有账号，去登录</el-link
          >
        </el-form-item>
      </el-form>
      <el-form
        v-else
        :model="loginForm"
        :rules="rules"
        size="large"
        autocomplete="off"
      >
        <el-form-item>
          <h1 class="form-title">登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item class="flex">
          <el-checkbox style="color: white">记住我</el-checkbox>
          <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
          <el-link type="primary" :underline="false" style="color: red"
            >忘记密码？</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="button" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link
            @click="
              isRegister = true;
              clearData();
            "
            :underline="false"
            class="switch-link"
            >没有账号？立即申请账号</el-link
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { loginAPI, signIn } from "../api/service.js";
import { useTokenStore } from "../store/token.ts";

const token = useTokenStore();

const router = useRouter();

const isRegister = ref(false);

const loginForm = reactive({
  username: "admin",
  password: "123456",
});

const register = reactive({
  username: "",
  password: "",
  code: "",
});

const clearData = () => {
  (loginForm.username = ""),
    (loginForm.password = ""),
    (register.username = ""),
    (register.password = ""),
    (register.code = "");
};

const login = async () => {
  // console.log("登录")
  try {
    const result = await loginAPI(loginForm);
    // console.log(result)
    if (result.data.code === 200) {
      token.setToken(result.data.token);
      ElMessage.success(result.data.msg ? result.data.msg : "登录成功");
      // router.push('/supervisory')
      router.push("/supervisory");
    } else {
      ElMessage.error(result.data.msg ? result.data.msg : "账号或密码错误");
    }
  } catch (error) {
    ElMessage.error("请检查网络或联系管理员");
  }
};

const registerAPI = async () => {
  try {
    console.log(register);
    const result = await signIn(register);
    if (result.data.code === 200) {
      ElMessage.success(result.data.msg ? result.data.msg : "注册成功");
      clearData();
      isRegister.value = false;
    } else {
      ElMessage.error(result.data.msg ? result.data.msg : "注册失败");
    }
  } catch (error) {
    ElMessage.error("请检查网络或联系管理员");
  }
};

const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      min: 5,
      max: 20,
      message: "用户名长度在 5 到 20 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在 6 到 20 个字符", trigger: "blur" },
  ],
  code: [
    { required: true, message: "请输入邀请码", trigger: "blur" },
    { min: 6, max: 20, message: "邀请码长度为8个字符", trigger: "blur" },
  ],
});
</script>

<style lang="scss" scoped>
.title {
  font-size: 120px;
  margin-top: 20px;
  text-align: center;
  font-family: "STXingkai";
  font-weight: bold;
  color: rgb(17, 11, 13);
  user-select: none;
  margin-top: 190px;
}

.login-page {
  height: 100vh;
  background-color: #b4e7ee;
  background: url("../assets/download.jpg") no-repeat center / cover;

  .bg {
    background: url("../assets/download\ \(1\).jpg") no-repeat center / cover;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .form-title {
      margin: 0 auto;
      font-family: "STLiti";
      font-size: 65px;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

    .switch-link {
      color: white;
      font-family: "STFangsong";
    }
  }
}
</style>
